#include<iostream>
#include"util.hpp"
#include"level.hpp"
#include"format.hpp"
#include"logsink.hpp"
#include"logger.hpp"
#include"buffer.hpp"
#define TOSTRING(name) #name

//测试全局日志器类
void test(){
    bitlog::Logger::ptr logger=bitlog::loggerManager::getInstance().getLogger("singleton_logger");
    logger->debug(__FILE__,__LINE__,"%s","全局日志器测试");
    logger->info(__FILE__,__LINE__,"%s","全局日志器测试");
    logger->warning(__FILE__,__LINE__,"%s","全局日志器测试");
    logger->error(__FILE__,__LINE__,"%s","全局日志器测试");
    logger->fatal(__FILE__,__LINE__,"%s","全局日志器测试");

    bitlog::Logger::ptr root_logger=bitlog::loggerManager::getInstance().getrootLogger();
    root_logger->debug(__FILE__,__LINE__,"%s","全局日志器测试");
    root_logger->info(__FILE__,__LINE__,"%s","全局日志器测试");
    root_logger->warning(__FILE__,__LINE__,"%s","全局日志器测试");
    root_logger->error(__FILE__,__LINE__,"%s","全局日志器测试");
    root_logger->fatal(__FILE__,__LINE__,"%s","全局日志器测试");
}
int main()
{
    //util类测试
    // cout<<bitlog::util::file::exists("util.hpp")<<endl;
    // bitlog::util::file::create_dir("./a/b/c");
    // cout<<bitlog::util::file::path("./a/b")<<endl;

    //level类测试
    // cout<<bitlog::Loglevel::toString(bitlog::Loglevel::value::FATAL)<<endl;

    //format类测试
    // bitlog::LogMsg msg("root",bitlog::Loglevel::value::WARNING,"测试文件",12,"format类测试");
    // bitlog::Formatter::ptr formatter=make_shared<bitlog::Formatter>();
    // string str=formatter->format(msg);
    // cout<<str<<endl;

    //Logsink类测试
    // bitlog::LogMsg msg("root",bitlog::Loglevel::value::WARNING,"测试文件",12,"format类测试");
    // bitlog::Formatter formatter;
    // string str=formatter.format(msg);
    // bitlog::LogSink::ptr std_lsp=bitlog::SinkFactor::createSink<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp=bitlog::SinkFactor::createSink<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp=bitlog::SinkFactor::createSink<bitlog::RollSink>("./logfile/test.log-",1024*1024);
    // std_lsp->log(str.c_str(),str.size());
    // file_lsp->log(str.c_str(),str.size());
    // size_t cur=0;
    // while(cur<10*1024*1024){
    //     roll_lsp->log(str.c_str(),str.size());
    //     cur+=str.size();
    // }

    //logger类测试
    // string name="Synclogger";
    // bitlog::Loglevel::value limit=bitlog::Loglevel::value::ERROR;
    // bitlog::Formatter::ptr format=make_shared<bitlog::Formatter>();
    // bitlog::LogSink::ptr std_lsp=bitlog::SinkFactor::createSink<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp=bitlog::SinkFactor::createSink<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp=bitlog::SinkFactor::createSink<bitlog::RollSink>("./logfile/test.log-",1024*1024);
    // vector<bitlog::LogSink::ptr> sinks={std_lsp,file_lsp,roll_lsp};
    // bitlog::Logger::ptr logger(new bitlog::Synclogger(name,format,sinks,limit));
    // logger->debug(__FILE__,__LINE__,"%s","测试文件");
    // logger->info(__FILE__,__LINE__,"%s","测试文件");
    // logger->warning(__FILE__,__LINE__,"%s","测试文件");
    // logger->error(__FILE__,__LINE__,"%s","测试文件");
    // logger->fatal(__FILE__,__LINE__,"%s","测试文件");
    //---------------------------------------------------------------------------
    //建造者模式
    // bitlog::LocalLoggerBuilder::ptr builder(new bitlog::LocalLoggerBuilder());
    // builder->buildLoggerName("sync_logger");
    // builder->buildLimitLevel(bitlog::Loglevel::value::DEBUG);
    // builder->buildLoggerType(bitlog::Type::LOGGER_SYNC);
    // builder->buildSink<bitlog::StdoutSink>();
    // builder->buildSink<bitlog::FileSink>("./logfile/test.log");
    // builder->buildSink<bitlog::RollSink>("./logfile/test.log-",1024*1024);
    // bitlog::Logger::ptr logger=builder->build();
    // logger->debug(__FILE__,__LINE__,"%s","测试文件");
    // logger->info(__FILE__,__LINE__,"%s","测试文件");
    // logger->warning(__FILE__,__LINE__,"%s","测试文件");
    // logger->error(__FILE__,__LINE__,"%s","测试文件");
    // logger->fatal(__FILE__,__LINE__,"%s","测试文件");
    // string str="测试日志";
    // size_t cursize=0;
    // while(cursize<(1024*1024-str.size())){
    //     logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    //     cursize+=str.size();
    // }

    //buffer类测试
    // ifstream ifs("./logfile/test.log",ios::binary);
    // if(ifs.is_open()==false){
    //     cout<<"打开文件失败"<<endl;
    //     return -1;
    // }
    // ifs.seekg(0,ios::end);
    // size_t fsize=ifs.tellg();
    // ifs.seekg(0,ios::beg);
    // string body;
    // body.resize(fsize);
    // ifs.read(&body[0],fsize);
    // if(ifs.good()==false){
    //     cout<<"read error"<<endl;
    //     return -1;
    // }
    // ifs.close();
    // bitlog::Buffer buffer;
    // buffer.push(&body[0],body.size());
    // ofstream ofs("./logfile/tmp.log",ios::binary);
    // ofs.write(buffer.begin(),buffer.readAblesize());
    // if(ofs.good()==false){
    //     cout<<"write error"<<endl;
    //     return -1;
    // }
    // ofs.close();

    //logger--looper 异步日志器类测试
    // bitlog::Builder::ptr builder(new bitlog::LocalLoggerBuilder());
    // builder->buildLoggerName("Async-logger");
    // builder->buildLimitLevel(bitlog::Loglevel::value::DEBUG);
    // builder->buildLoggerType(bitlog::Type::LOGGER_ASYNC);
    // builder->buildSink<bitlog::StdoutSink>();
    // // builder->buildSink<bitlog::FileSink>("./logfile/test.log");
    // // builder->buildSink<bitlog::RollSink>("./logfile/test.log-",1024*1024);
    // bitlog::Logger::ptr logger=builder->build();
    // logger->debug(__FILE__,__LINE__,"%s","异步日志器类测试");
    // logger->info(__FILE__,__LINE__,"%s","异步日志器类测试");
    // logger->warning(__FILE__,__LINE__,"%s","异步日志器类测试");
    // logger->error(__FILE__,__LINE__,"%s","异步日志器类测试");
    // logger->fatal(__FILE__,__LINE__,"%s","异步日志器类测试");

    //全局日志器类测试
    bitlog::Builder::ptr builder(new bitlog::GlobalLoggerBuilder());
    builder->buildLimitLevel(bitlog::Loglevel::value::DEBUG);
    builder->buildLoggerType(bitlog::Type::LOGGER_ASYNC);
    builder->buildLoggerName("singleton_logger");
    builder->buildSink<bitlog::StdoutSink>();
    builder->buildSink<bitlog::FileSink>("./logfile/test.log");
    builder->build();
    test();

    return 0;
}
